{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Optimizer\n",
    "\n",
    "In gradient-base optimization algorithms, we update the parameters (or weights) using the gradients in each iteration. We call this updating function as `Optimizer`. \n",
    "\n",
    "The main method of an optimizer is `update(weight, grad)`, which updates a NDArray weight using a NDArray gradient. But given that a multi-layer neural network often has more than one weights, we assign each weight a unique integer index. Furthermore, an optimizer may need space to store auxiliary state, such as momentum, we also allow a user-defined state for updating. In summary, an optimizer has two major methods\n",
    "\n",
    "- `create_state(index, weight)`: create auxiliary state for the `index`-th weight. \n",
    "- `update(index, weight, grad, state)`: update the `index`-th weight given the gradient and auxiliary state. The state can be also updated.\n",
    "\n",
    "\n",
    "## Basic Usage\n",
    "\n",
    "### Create and Update\n",
    "MXNet has already implemented several popular optimizers in [python/mxnet/optimizer.py](https://github.com/dmlc/mxnet/blob/master/python/mxnet/optimizer.py). An convenient way to create one is by using `optimizer.create(name, args...)`. The following codes create a standard SGD updater which does\n",
    "\n",
    "```\n",
    "weight = weight - learning_rate * grad\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import mxnet as mx\n",
    "opt = mx.optimizer.create('sgd', learning_rate=.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then we can use the `update` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.89999998  0.89999998  0.89999998]\n",
      " [ 0.89999998  0.89999998  0.89999998]]\n"
     ]
    }
   ],
   "source": [
    "grad = mx.nd.ones((2,3))\n",
    "weight = mx.nd.ones((2,3))\n",
    "index = 0\n",
    "opt.update(index, weight, grad, state=None)\n",
    "print(weight.asnumpy())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When momentum is non-zero, the sgd optimizer needs extra state. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.1 -0.1 -0.1]\n",
      " [-0.1 -0.1 -0.1]]\n"
     ]
    }
   ],
   "source": [
    "mom_opt = mx.optimizer.create('sgd', learning_rate=.1, momentum=.01)\n",
    "state = mom_opt.create_state(index, weight)\n",
    "opt.update(index, weight, grad, state)\n",
    "print(state.asnumpy())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Flexible Learning Rate\n",
    "\n",
    "- [lr scheduler](https://github.com/dmlc/mxnet/blob/master/python/mxnet/lr_scheduler.py)\n",
    "- layer-wise lr: set_lr_mult, set_wd_mult\n",
    "\n",
    "\n",
    "### More optimizers\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Customized Optimizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
